﻿using System;
using System.Windows.Forms;
using CSW.Framework.Common;
using CSW.Framework.Common.Binding.ValueTypes;

namespace CSW.Framework.UI.Binding
{
    /// <summary>
    /// Wraps the <see cref="ErrorProvider" /> error provider.  See <see cref="Binder" />.
    /// </summary>
    public class ErrorWrapper : IErrorWrapper
    {
        private readonly ErrorProvider m_ErrorProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorWrapper"/> class.
        /// </summary>
        /// <param name="errorProvider">The <see cref="ErrorProvider"/>.</param>
        public ErrorWrapper(ErrorProvider errorProvider)
        {
            Guard.ArgumentNotNull(errorProvider, "errorProvider");

            m_ErrorProvider = errorProvider;
        }

        #region IErrorProvider Members

        /// <summary>
        /// Sets the error of a control.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="text">The error text.</param>
        /// <param name="errorType">The type of error.</param>
        public void SetError(Control control, string text, ErrorType errorType)
        {
            Guard.ArgumentNotNull(control, "control");

            switch (errorType)
            {
                case ErrorType.None:
                    m_ErrorProvider.Clear();
                    break;

                case ErrorType.Warning:
                    m_ErrorProvider.SetError(control, text);
                    break;

                case ErrorType.Information:
                    m_ErrorProvider.SetError(control, text);
                    break;

                case ErrorType.Error:
                    m_ErrorProvider.SetError(control, text);
                    break;

                default:
                    throw new Exception(string.Format("Unhandled ErrorType '{0}'", errorType));
            }
        }

        /// <summary>
        /// Clears the error of a control.
        /// </summary>
        /// <param name="control">The control.</param>
        public void ClearError(Control control)
        {
            Guard.ArgumentNotNull(control, "control");

            SetError(control, null, ErrorType.None);
        }

        #endregion
    }
}
